﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace cnFederal
{
    
    public class CnfiLinks
    {
        private static CnfiDataDataContext lnq;
        private static List<CnfiLink> _links;

        public CnfiLinks()
        { }

        public List<CnfiLink> Links
        {
            get
            {
                return _links;
            }
            set
            {
                _links = value;
            }
        }

        public static void Load()
        {
            lnq = new CnfiDataDataContext();
            _links = new List<CnfiLink>();
            if (_links != null)
            {
                List<CnfiLink> cLinks = new List<CnfiLink>();
                var b = from c in lnq.Links
                        select c;

                foreach (var s in b)
                {
                    CnfiLink news = new CnfiLink();
                    news.Id = (int)s.Id;
                    news.EkxternalTableId = (int)s.EksternTabelId;
                    news.PostId = (int)s.PostId;
                    news.LinkUrl = (string)s.LinkUrl;
                    news.ImageUrl = (string)s.ImageUrl;
                    news.Title = (string)s.Title;
                    news.Description = (string)s.Desctiption;
                    if (s.DateRegistered != null)
                        news.DateRegistered = (DateTime)s.DateRegistered;
                   
                 
                    cLinks.Add(news);

                }

                _links = cLinks;
            }
        }

        public void RefreshLinks()
        {
            _links.Clear();
            _links = GetAllFaLinks();
        }

        public static List<CnfiLink> GetAllFaLinks()
        {

            return _links;
        }

        public static List<CnfiLink> GetTopLinks(int number)
        {
            if (_links == null) return null;
            return _links.Take(number).ToList();
        }

        public static CnfiLink GetFaLinkById(int id)
        {
            if (_links == null) return null;
            CnfiLink link = null;
            var b = from c in _links
                    where c.Id == id
                    select c;
            foreach (var s in b)
            {
                link = new CnfiLink();
                link.Id = (int)s.Id;
                link.EkxternalTableId = (int)s.EkxternalTableId;
                link.PostId = (int)s.PostId;
                link.LinkUrl = (string)s.LinkUrl;
                link.ImageUrl = (string)s.ImageUrl;
                link.Title = (string)s.Title;
                link.Description = (string)s.Description;
                if (s.DateRegistered != null)
                    link.DateRegistered = (DateTime)s.DateRegistered;
                   
            }

            return link;
        }


        public static int CreateNew(CnfiLink lenke)
        {
            int lastId = 0;
            try
            {
                Link lk = new Link();
                lk.EksternTabelId = lenke.EkxternalTableId;
                lk.PostId = lenke.PostId;
                lk.LinkUrl = lenke.LinkUrl;
                lk.ImageUrl = lenke.ImageUrl;
                lk.Title = lenke.Title;
                lk.Desctiption = lenke.Description;
                lk.DateRegistered = lenke.DateRegistered;  
             
                lnq.Links.InsertOnSubmit(lk);
                lnq.SubmitChanges();
                lnq.Connection.Close();

                lastId =(int)( from c in lnq.Links
                        select c.Id).Max();
                
            }
            catch (Exception ex)
            {
               
            }
            return lastId;
        }

        public static bool DeleteItem(int id)
        {
            bool deleted = false;
            var b = from f in lnq.Links
                    where f.Id == id
                    select f;
            if (b != null && b.Count() > 0)
            {
                Link n = b.First();
                try
                {
                    lnq.Links.DeleteOnSubmit(n);
                    lnq.SubmitChanges();
                    deleted = true;
                }
                catch (Exception ex)
                {
                    deleted = false;
                }
            }
            return deleted;
        }
        public static void Save(CnfiLink lenke)
        {
            var b = from f in lnq.Links
                    where f.Id == lenke.Id
                    select f;
            foreach (var link in b)
            {
                link.Id = (int)lenke.Id;
                link.EksternTabelId = (int)lenke.EkxternalTableId;
                link.PostId = (int)lenke.PostId;
                link.LinkUrl = (string)lenke.LinkUrl;
                link.ImageUrl = (string)lenke.ImageUrl;
                link.Title = (string)lenke.Title;
                link.Desctiption = (string)lenke.Description;
                if (lenke.DateRegistered != null)
                    link.DateRegistered = (DateTime)lenke.DateRegistered;
            }
            lnq.SubmitChanges();
        }


    }

    public class CnfiLink
    {
        public int Id
        {
            get;
            set;
        }
        public int EkxternalTableId
        {
            get;
            set;
        }
        public int PostId
        {
            get;
            set;
        }
        public string LinkUrl
        {
            get;
            set;
        }

        public string ImageUrl
        {
            get;
            set;
        }
        public string Title
        {
            get;
            set;
        }
        public string Description
        {
            get;
            set;
        }

        public DateTime DateRegistered
        {
            get;
            set;
        }


    }














}